//
//  WBHomeViewController.swift
//  CFZWeiBo
//
//  Created by Cedric Zheng on 23/11/2016.
//  Copyright © 2016 Cedric Zheng. All rights reserved.
//

import UIKit

let cellIdentifier = "HomeCell"

class WBHomeViewController: WBRootViewController {
    
    lazy var cellDataSource: [WBStatusViewModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        visitorView?.setVisitorView(isAnimation: true, image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"), title: "关注一些人，回这里看看有什么惊喜")
        
        loadStatus()
    }
    
}

extension WBHomeViewController {
    
    override func setupUI() {
        super.setupUI()
        tableView.register(WBStatusCell.self, forCellReuseIdentifier: cellIdentifier)
        tableView.estimatedRowHeight = 300.0
        tableView.rowHeight = UITableViewAutomaticDimension
    }
    
}

extension WBHomeViewController {
    
    func loadStatus() {
        networkToolShared.requestStatus(access_token: userAccountShared.access_token, callBack: { (responseObject) in
            if
                let statusObject        = responseObject as? [String: Any],
                let statusDicArray      = statusObject["statuses"] as? [[String: Any]],
                let statusModelArray    = NSArray.yy_modelArray(with: WBStatusModel.self, json: statusDicArray) as? [WBStatusModel]
            {
                var statusViewModelArray: [WBStatusViewModel] = []
                for model in statusModelArray {
                    let viewModel: WBStatusViewModel = WBStatusViewModel(model)
                    statusViewModelArray.append(viewModel)
                }
                self.cellDataSource += statusViewModelArray
                
                self.tableView.reloadData()
                self.refreshControl.endRefreshing()
            }
        })
    }
    
}

extension WBHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellDataSource.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! WBStatusCell
        
        cell.statusViewModel = cellDataSource[indexPath.row]
        
        return cell
    }
    
}
